<?php
namespace Badmins\Model;
use Think\Model;
class PromotionModel extends Model{
    protected $_validate = array(
			array('name','require','活动名称必须'),
			array('start_time','require','活动开始时间必须'),
			array('end_time','require','活动结束时间必须'),
    		array('award_type','number','活动方式必须'),
    		array('conditions','number','订单总额必须'),
    		array('user_group','user_group','请选择适用范围',1,'callback',3),
    		array('proid','goodsid','请选择适用商品',1,'callback',3),
    		array('award_value','awardvalue','请选择活动方式所对应的值',1,'callback',3),
			array('start_time,end_time','cktime','结束时间必须大于开始时间',1,'callback',3),
	);
    protected $_auto = array (
    		array ('user_group','user_group',3,'callback',1),
    		array('goods_id','goodsid',3,'callback',1),
    		array('award_value','awardvalue',3,'callback',1),
    );
	public function cktime($data){
		if($data['end_time']<$data['start_time']){
			return false;
		}else{
			return true;
		}
	}
	public function user_group(){
		$groupid = I('user_group');
		if(empty($groupid[0])){
			return false;
		}else{
			$flag = 1;
			foreach ($groupid as $va){
				if($va == "all"){
					$flag *= 0;
					break;
				}else{
					$flag *= 1;
				}
			}
			if($flag){
				return join(",",$groupid);
			}else{
				return "all";
			}
		}
	}
	public function goodsid(){
		$goodsid = I('proid');
		$type = I('type');
		if($type){
			return true;
		}else{
			if($goodsid){
				return join(",",$goodsid);
			}else{
				return false;
			}
		}
	}
	public function awardvalue(){
		$award_type = I('award_type/d');
		if($award_type == 5){
			return I('conditions/d');
		}else{
			$name = 'award_value_'.$award_type;
			$value = I($name);
			if(empty($value)){
				return false;
			}else{
				return $value;
			}
		}
		
	}
	
}